﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ERMSys.Framework.Utilities;
using Se7en.Admin.Common;
using Se7en.Admin.Model;
using Se7en.Admin.Business;

namespace Se7en.Admin.Web.Pages
{
    public partial class ManageRole : AdminPageBase
    {
        #region Properties

        #endregion

        #region GUI Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (!IsPostBack)
            {
                BindData();
                BindRoleType();
            }
        }

        protected void rdoIsDefault_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdo = sender as RadioButton;
            DBContext.Role_UpdateDefaultRole(rdo.ValidationGroup.ConvertToInt32());
            DBContext.SaveAllChanges();
        }

        protected void ddlRoleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void grdRole_Delete(object sender, ERMSys.Web.Controls.GridViewDeleteRowsEventArgs e)
        {

        }

        protected void grdRole_AddNew(object sender, EventArgs e)
        {
            ctlRoleEdit.ClearData(ddlRoleType.SelectedValue);
            ctlRoleEdit.Mode = Modes.Add;
            modalRole.Show();
        }

        protected void grdRole_ActionClick(object sender, ERMSys.Web.Controls.GridViewActionClickArgs e)
        {
            int roleId = (int)e.Key.Value;
            switch (e.ActionName)
            {
                case "Edit":
                    ctlRoleEdit.BindRoles(roleId);
                    ctlRoleEdit.Mode = Modes.Edit;
                    modalRole.Show();
                    break;
                case "Delete":
                    DBContext.Role_Delete(roleId);
                    try
                    {
                        DBContext.SaveAllChanges();
                        lblError.Text = string.Format(MessageRes.Success, "Delete");
                    }
                    catch
                    {
                        lblError.Text = string.Format(MessageRes.Failed, "Delete"); ;
                    }
                    BindData();
                    break;
            }
        }

        protected void grdRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            TemplateField cols = this.grdRole.Columns[2] as TemplateField;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                cols.Visible = ddlRoleType.SelectedValue.ConvertToInt32() != (int)RoleType.Admin;
            }
        }

        protected void ctlRoleEdit_Success(object sender, EventArgs e)
        {
            lblError.Text = string.Format(MessageRes.Success, "Save");
            modalRole.Hide();
            BindData();
        }

        protected void grdRole_Refresh(object sender, EventArgs e)
        {
            BindData();
        }

        protected void grdRole_PageIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #endregion

        #region Methods

        public void BindData()
        {
            int totalRecord = 0;
            List<Role> roles = DBContext.Role_GetAll(ddlRoleType.SelectedValue.ConvertToInt32(), "", grdRole.PageIndex, grdRole.PageSize, out totalRecord);
            grdRole.DataBind(roles, totalRecord);
        }

        protected bool GetIsDefaultRole(object roleId)
        {
            Role role = DBContext.Role_GetRoleById(roleId.ConvertToInt32());
            return role == null ? false : role.IsDefault;
        }

        private void BindRoleType()
        {
            ddlRoleType.DataSource = EnumHelper.GetEnumData(typeof(RoleType));
            ddlRoleType.DataTextField = "Text";
            ddlRoleType.DataValueField = "Value";
            ddlRoleType.DataBind();
        }

        #endregion
    }
}